package edu.hunau.recommend.controller;

import edu.hunau.recommend.common.R;
import edu.hunau.recommend.common.ResultCodeEnum;
import edu.hunau.recommend.exception.RecommendBusinessException;
import edu.hunau.recommend.pojo.Category;
import edu.hunau.recommend.pojo.Shop;
import edu.hunau.recommend.service.CategoryService;
import edu.hunau.recommend.service.ShopService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author name
 * @creater 2022-09-21-10:18
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Resource
    private ShopService shopService;

    @Resource
    private CategoryService categoryService;

    @RequestMapping("/recommend")
    public R recommend(@RequestParam(name = "longitude") BigDecimal longitude,@RequestParam(name = "latitude") BigDecimal latitude){
        if (Objects.isNull(longitude) || Objects.isNull(latitude)){
            throw new RecommendBusinessException(ResultCodeEnum.REQUEST_PARAM_ERROR);

        }
        List<Shop> shops = shopService.recommend(longitude, latitude);
        return R.ok().data("List",shops);
    }

    @RequestMapping("/search")
    public R search(@RequestParam(name = "longitude") BigDecimal longitude,
                    @RequestParam(name = "latitude") BigDecimal latitude,
                    @RequestParam(name = "keyword") String keyword,
                    @RequestParam(name = "orderby", required = false) Integer orderby,
                    @RequestParam(name = "categoryId", required = false) Integer categoryId,
                    @RequestParam(name = "tags", required = false) String tags
                     ){
        if (Objects.isNull(longitude) || Objects.isNull(latitude)){
            throw new RecommendBusinessException(ResultCodeEnum.REQUEST_PARAM_ERROR);

        }
        List<Shop> shops = shopService.search(longitude, latitude, keyword, orderby, categoryId,tags);
        List<Category> categories = categoryService.selectAll();
        List<Map<String, Object>> tagsResult = shopService.searchGroupbyTags(keyword, categoryId, tags);
        return R.ok().data("List",shops).data("categoryList",categories).data("tags",tagsResult);
    }
}
